const app = getApp(); //引入首页
const urls = "https://wx.amagich.com/";
let version = wx.setStorageSync('version');
/**
 * @xk 如果没有系统版本则重新进行版本的读取
 */
var result = '';
if(version == null || version == ''){
    wx.request({
        url: urls + 'getSet',
        method: "POST",
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res){
            if(res.data.code == 200){
                wx.setStorageSync('version', res.data.resultData.version);
                wx.setStorageSync('app_name', res.data.resultData.app_name);
                wx.setStorageSync('app_logo', res.data.resultData.app_logo);
                wx.setStorageSync('phone', res.data.resultData.phone);
                result = urls+res.data.resultData.version;
                wx.setStorageSync('http_request', result);
            }
        }
    })
}else{
    result = urls + wx.setStorageSync('version');
    wx.setStorageSync('http_request', result);
}
/**
 * @xk 重新赋值进行调用
 */
const http_url = wx.getStorageSync('http_request');

let post = (url, data = {}, hidden, msg='加载中') => {
    let promise = new Promise((success, fail) => {
        wx.request({
            url: urls + 'v1/' + url,
            data: data,
            header: {
                "Content-Type": "application/json",
                "token": wx.getStorageSync('token') || '',
                "Cookie": wx.getStorageSync('cookieKey') || '',
            },
            method: 'POST',
            success: res => {
                if (res.data.code == 200) {
                    if(res && res.header && res.header['Set-Cookie']){
                        wx.setStorageSync('cookieKey', res.header['Set-Cookie']);//保存Cookie到Storage
                    }
                    getToken(res)
                    success(res.data.resultData)

                } else if (res.data.code == 203) {
                    clearLoginState()
                } else if (res.data.code == -201) {
                    clearLoginState()
                } else if (res.data.code == -202) {
                    return
                } else if(res.data.code == 202) {
                    getToken(res)
                    return
                } else if(res.data.code == 204){
                    getToken(res)
                    success(res.data.resultData)
                    // app.showToast(res.data.msg, () => {})
                }else if(res.data.code == 205){
                    getToken(res)
                    success(res.data.resultData)
                    app.showToast(res.data.msg, () => {})
                } else {
                    app.showToast(res.data.msg, () => {
                        wx.hideLoading()
                    })
                }
            },
            fail: res => {
            },
            complete: () => {
                wx.hideNavigationBarLoading()
                wx.stopPullDownRefresh()
            }
        })
    });
    return promise;
};

let getToken = (res) => {
    if (res.header.token == '500' || res.header.Token == '500') {
        clearLoginState()
    }
    if (res.header.Token && res.header.Token != '') {
        wx.setStorageSync('token', res.header.Token)
        app.globalData.token = res.header.Token
    }
    if (res.header.token && res.header.token != '') {
        wx.setStorageSync('token', res.header.token)
        app.globalData.token = res.header.token
    }
};
/**
 * 清空登录状态
 */
let clearLoginState = () => {
    let version = wx.getStorageSync('version');
    let app_name = wx.getStorageSync('app_name'),
        app_logo = wx.getStorageSync('app_logo');
    wx.clearStorageSync();
    wx.setStorageSync('version', version);
    wx.setStorageSync('app_name', app_name);
    wx.setStorageSync('app_logo', app_logo);
    app.globalData.user_id = ''
    app.globalData.phone = ''
    app.globalData.openid = ''
    app.globalData.unionId = ''
    app.globalData.token = ''
    wx.switchTab({
        url: '/pages/my/my'
    })
};


module.exports = {
    url: urls + 'v1/',
    post,
    clearLoginState,
}
